<?php 

Class goodsModel extends appModel
{
	
	function get_goods_list($args)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$page_size=isset($args['page_size'])?$args['page_size']:10;
		$id=isset($args['cat_id'])?$args['cat_id']:0;
		$page=isset($args['page'])?$args['page']:1;
		
		$sql="SELECT parent_id FROM ".$this->table('category')." WHERE id = '".$id."'";
		$parent_id=$this->getOne($sql);
		$where=" WHERE goods_status=1 ";
		if($id)
		{
			
				$sql="SELECT id FROM ".$this->table('category')." WHERE parent_id = '".$id."'";
				$temp=$this->getAll($sql);
				$id_arr='(';
				foreach($temp as $k=>$v)
				{
					$id_arr.=$v[id].",";
				}
				$id_arr.=$id.")";
				$where.=' AND cat_id in'.$id_arr;
			 
		}	
		if($args['keyword'])
		{
			$where.=" AND goods_name like '%".$args['keyword']."%'";
		}	
		
				$result=$this->show_page("SELECT id,goods_name,goods_thumb,goods_brief FROM ".$this->table('goods').$where.'   ORDER BY ordering',$page,$page_size,$cfg);
				if($result)
				{
					foreach($result['result'] as $k=>$v)
					{
						$result['result'][$k]['goods_name']=hy_dig_filter($v['goods_name']);
						$result['result'][$k]['goods_brief']=hy_dig_filter($v['goods_brief']);
						if(REWRITE==1)
						{
							$result['result'][$k]['url']="/g/".$v['id'].".html";
						}
						else
					    {
						   $result['result'][$k]['url']="/index.php?g=home&m=goods&a=view&id=".$v['id'];
					    }
					}
				}
				
				$listPage['pagelink']=$result['page'];
	            $listPage['data']=$result['result'];
			
			
		
		return $listPage;
	}
	public function get_link_goods($id)
	{
		$sql='SELECT g.goods_brief,g.goods_thumb,g.goods_name,g.id FROM '.$this->table('link_goods').' as lg,'.$this->table('goods')." as g WHERE lg.link_goods=g.id AND lg.goods_id='".$id."'";
	    $result= $this->getAll($sql);
	    foreach($result as $k=>$v)
	    {
	    	$result[$k]['goods_name']=hy_dig_filter($v['goods_name']);
	    	$result[$k]['goods_brief']=hy_dig_filter($v['goods_brief']);
	    	if(REWRITE==1)
	    	{
	    		$result[$k]['url']="/g/".$v['id'].".html";
	    	}
	    	else
	    	{
	    		$result[$k]['url']="/index.php?g=home&m=goods&a=view&id=".$v['id'];
	    	}
	    }
	    return $result;
	}
	
	
	public function get_good($id)
	{
		$result=array();
		if($id)
		{
		   $sql="SELECT g.* FROM ".$this->table('goods')." as g  WHERE id = '".$id."'";
			
		   $result=$this->getRow($sql);
		   if($result)
		   {
		//   	print_r($result['external_chain']);
		   	$result['external_chain']=explode(PHP_EOL,$result['external_chain']);
		   	$result['goods_name']=hy_dig_filter($result['goods_name']);
		   	$result['goods_brief']=hy_dig_filter($result['goods_brief']);
		   	$result['content']=stripslashes(hy_dig_filter($result['content']));
		   	//获取品牌
		   	$sql="SELECT brand_name FROM ".$this->table('goods_brand')."   WHERE id = '".$result['brand_id']."'";
		   	
		   	$result['brand_name']=hy_dig_filter($this->getOne($sql));
		   	//获取相册集
		$sql="SELECT p.* FROM ".$this->table('goods_gallery')." as p WHERE p.goods_id = '".$id."'";
		
		$photo=$this->getAll($sql);
		$result['gallery']=$photo;
		//获取属性
		$sql='SELECT ge.attr_id,ge.attr_value,ge.goods_img,ga.name FROM '.$this->table('goods_attr_extend').' as ge,'.$this->table('goods_attr')." as ga WHERE ga.id=ge.attr_id AND ge.goods_id='".$id."'";
		$res= $this->getAll($sql);
		$attr=array();
		foreach($res as $k=>$v)
		{
			$attr[$v['attr_id']]['name']=hy_dig_filter($v['name']);
			$temp=(array)json_decode($v['attr_value']);
			$attr[$v['attr_id']]['value'][$k]['attr_text']=hy_dig_filter(urldecode($temp['name']));;
			$attr[$v['attr_id']]['value'][$k]['goods_img']=$v['goods_img'];
			if($temp['color']!='')
			{
				$attr[$v['attr_id']]['value'][$k]['type']='1';
				$attr[$v['attr_id']]['value'][$k]['attr_value']=$temp['color'];
			}
			elseif($temp['thumb']!='') 
			{
				$attr[$v['attr_id']]['value'][$k]['type']='2';
				$attr[$v['attr_id']]['value'][$k]['attr_value']=$temp['thumb'];
			}
			else 
			{
				$attr[$v['attr_id']]['value'][$k]['type']='3';
				$attr[$v['attr_id']]['value'][$k]['attr_value']=hy_dig_filter(urldecode($temp['name']));
			}
			
		}
		$result['attr']=$attr;
		//获取规格-start
		//$spec_arr=$this->get_goods_spec($res['goods_type']);
		
		$sql='SELECT spec_id,spec_value FROM '.$this->table('goods_spec_extend')." WHERE goods_id='".$id."'";
		$temp=$this->getAll($sql);
		foreach($temp as $k=>$v)
		{
			$sql='SELECT name  FROM '.$this->table('goods_spec')." WHERE id='".$v['spec_id']."'";
			$goods_spec[$v['spec_id']]['name']=hy_dig_filter($this->getOne($sql));
			$goods_spec[$v['spec_id']]['value']=hy_dig_filter($v['spec_value']);
		}
		$result['spec']=$goods_spec;
		
			return $result;
		   }
		   else
		  {
			return $result;
		   }
		}
		
		return $result;
	}
	
}